extends Node2D

@onready var flash: CanvasLayer = $flash
@onready var blueprint: CanvasLayer = $blueprint
@onready var camera: Node2D = $outer_world/camera
@onready var dialog: Node2D = $outer_world/camera/dialog
@onready var inner_world: Node2D = $inner_world
@onready var outer_world: Node2D = $outer_world
@onready var interact: Control = $outer_world/interact

enum scene{inner,outer,bulid}
var cur_scene=scene.inner
var is_chatting=true

func _ready() -> void:
	cur_scene=scene.outer
	_chat_in()

func _chat_in():
	dialog.visible=true
	is_chatting=true
	inner_world.heart.moveable=false
	inner_world.heart.timer.paused=true
	dialog.is_chatting=true
	for i in interact.get_children():
		i.disabled=true
	dialog._continue()

func _chat_out():
	dialog.visible=false
	is_chatting=false
	inner_world.heart.moveable=true
	inner_world.heart.timer.paused=false
	dialog.is_chatting=false
	for i in interact.get_children():
		i.disabled=false

func _change():
	if Input.is_action_just_pressed("change"):
		if cur_scene==scene.bulid:
			return
		if is_chatting:
			return
		flash.visible=true
		await flash._flash_out().finished
		match cur_scene:
			scene.inner:
				cur_scene=scene.outer
				outer_world._enter(inner_world._exit())
				if global.flag_list["level9_pass"]:
					global.cur_line="10"
					global.cur_index=0
					_chat_in()
				elif global.flag_list["level5_pass"]:
					if not global.flag_list["level6_enter"]:
						global.cur_line="6"
						global.cur_index=0
						_chat_in()
			scene.outer:
				cur_scene=scene.inner
				inner_world._enter(outer_world._exit())
				inner_world._reset()
				if not global.flag_list["level1_enter"]:
					global.flag_list["level1_enter"]=1
					global.cur_line="hint1"
					global.cur_index=0
					_chat_in()
		await flash._flash_in().finished
		flash.visible=false
	elif Input.is_action_just_pressed("build"):
		if cur_scene==scene.outer:
			return
		match cur_scene:
			scene.inner:
				inner_world._build_in()
				blueprint.visible=true
				await blueprint._blueprint_in().finished
				cur_scene=scene.bulid
			scene.bulid:
				inner_world._build_out()
				await blueprint._blueprint_out().finished
				blueprint.visible=false
				cur_scene=scene.inner

func _physics_process(delta: float) -> void:
	_change()
